package com.genzou.convert

class ConvertorTests extends GroovyTestCase {
    
    void testConvert() {
        def manager = new Convertor<Integer>()
        manager.with{
            filters << {
                it % 2 == 0
                
            }
            convertors << {
                it * 3
            }
        }
        
        def result = manager.convert([1,2,3,4])
        assertEquals([ 1, 6, 3, 12 ], result)
        assertEquals([ 6, 12 ], manager.convertedObjects)
        assertEquals([ 2,4 ], manager.backupObjects)
    }
    
    void testConvertSetNullObjects() {
        def manager = new Convertor<Integer>()
        manager.with{
            filters << {
                it % 2 == 0
                
            }
            convertors << {
                it * 3
            }
        }
        
        def result = manager.convert(null)
        assertEquals([], result)
        assertEquals([], manager.convertedObjects)
        assertEquals([], manager.backupObjects)
    }
    
    
    void testConvertSetManyFiltersAndConvertors() {
        def manager = new Convertor<Integer>()
        manager.with{
            filters << {
                it % 2 == 0
            }
            filters << {
                it % 3 == 0
            }
            convertors << {
                it * 3
            }
            convertors << {
                it * 2
            }
        }
        
        
        def result = manager.convert([1,2,3,4,5,6])
        assertEquals([ 1,2,3,4,5,36 ], result)
        assertEquals([ 36 ], manager.convertedObjects)
        assertEquals([ 6 ], manager.backupObjects)
    }
    
    
    void testConvertSetNullConvertor() {
        def manager = new Convertor<Integer>()
        manager.with{
            filters << {
                it % 2 == 0
            }
            convertors = null
        }
        
        def result = manager.convert([1,2,3,4,5])
        assertEquals([1,2,3,4,5], result)
        assertEquals([], manager.convertedObjects)
        assertEquals([], manager.backupObjects)
    }
    
    void testConvertSetCustomBackuper(){
        def manager = new Convertor<Integer>()
        manager.with{
            filters << {
                it % 2 == 0
                
            }
            convertors << {
                it * 3
            }
            backuper = {
                it - 10
            }
        }
        
        def result = manager.convert([1,2,3,4])
        assertEquals([ 1, 6, 3, 12 ], result)
        assertEquals([ 6, 12 ], manager.convertedObjects)
        assertEquals([ -8, -6 ], manager.backupObjects)
    }
}